package net.mimai.vultrue.bus.utils;

import net.mimai.vultrue.bus.entity.BaseItemDO;
import net.mimai.vultrue.framework.utils.FrameworkUtils;
import net.mimai.vultrue.framework.utils.JudgeUtils;
import net.mimai.vultrue.framework.utils.StringUtils;

/**
 * @ClassName EntityMyBatisUtil
 * @Description 和mybatis数据查询相关的处理
 * @Author 方丈
 * @Date 2021/2/3 10:11
 * @Version 1.0.0
 **/
public class EntityMyBatisUtil {

    final static int subNoLen = 3;

    final static String maxStr="999999999999999999999";
    final static String minStr="000000000000000000000";
    private static void initItemNoScope(BaseItemDO entity, int seqLen){
        if(JudgeUtils.isNotNull(entity.getBillNo()) && JudgeUtils.isNull(entity.getFirstItemNo())){
            String lNo = entity.getBillNo()+maxStr.substring(0,seqLen);
            String fNo = entity.getBillNo()+minStr.substring(0,seqLen);
            entity.setFirstItemNo(Long.valueOf(fNo));
            entity.setLastItemNo(Long.valueOf(lNo));
        }
    }

    /**
     * @description  因为明细表的billNo不是索引，根据billNo初始化itemNo的范围，提高查询效率，切面自行做，不需要代码显式调用
     * @author 方丈
     * @date 2021-02-03 10:12
     * @param
     * @return void
     * @since 1.0.0
     */
    public static void initBaseItemNoScope(BaseItemDO entity){
        initItemNoScope(entity,subNoLen);
    }

    private static Long obtainSubNo(Long no,int seq,int seqLen){
        if(seq<=0){
            FrameworkUtils.throwFailRet();
        }

        String itemNoSeq=String.valueOf(seq);
        String itemNoStr = no + StringUtils.leftPadIfNecessary(itemNoSeq,seqLen,'0');
        return Long.valueOf(itemNoStr);
    }

    public static Long obtainSubNo(Long no,int seq){
        return obtainSubNo(no,seq,subNoLen);
    }
}
